package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivityProSelPicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class ProSelPicActivity extends BaseAc<ActivityProSelPicBinding> {

    private SelPictureAdapter pictureAdapter;
    private List<SelectMediaEntity> listSel = new ArrayList<>();
    private boolean isSelectAll = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_sel_pic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProSelPicBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setEdit(true);
        pictureAdapter.setOnItemClickListener(this);
        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.tvStartPro.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSelectAll://全选
                listSel.clear();
                if (isSelectAll) {
                    for (SelectMediaEntity validDatum : pictureAdapter.getValidData()) {
                        validDatum.setChecked(false);
                    }
                } else {
                    for (SelectMediaEntity validDatum : pictureAdapter.getValidData()) {
                        validDatum.setChecked(true);
                        listSel.add(validDatum);
                    }
                }
                isSelectAll();
                pictureAdapter.notifyDataSetChanged();
                break;
            case R.id.tvStartPro://开始投屏
                List<String> listPath = new ArrayList<>();
                for (SelectMediaEntity selectMediaEntity : listSel) {
                    listPath.add(selectMediaEntity.getPath());
                }
                if (listPath.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_picture));
                    return;
                }
                ProPictureActivity.listPath = listPath;
                startActivity(ProPictureActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (pictureAdapter.getItem(position).isChecked()) {
            pictureAdapter.getItem(position).setChecked(false);
            listSel.remove(pictureAdapter.getItem(position));
            pictureAdapter.notifyDataSetChanged();
        } else {
            listSel.add(pictureAdapter.getItem(position));
            pictureAdapter.getItem(position).setChecked(true);
            pictureAdapter.notifyDataSetChanged();
        }
        isSelectAll();
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPicList.setVisibility(View.GONE);
                        mDataBinding.ivProSelPicNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPicList.setVisibility(View.VISIBLE);
                    mDataBinding.ivProSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPicList.setVisibility(View.GONE);
                    mDataBinding.ivProSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void isSelectAll() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listSel.size()));
        if (listSel.size() == pictureAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            isSelectAll = false;
        }
    }

}